// ==================================================================================
// Shared Genomics Project MPI Codebase
// Version 1.0 30/04/2010
//
// (c) 2010 University of Manchester all rights reserved
//
// This file is distributed under the GNU General Public License, Version 2.  
// Please see the file COPYING.txt for more details
// ==================================================================================

/*!
\file
\brief Implementation of BitString unpacking utility functions 
*/

#include <stdio.h>
#include <stdlib.h>

#include "bitstring.h"
#include "bitstring_unpack.h"

char* bitstring_unpack(char *data, int nRows, int nCols, int recordSizeInBytes) {
	int i, j;
	unsigned long int k = 0;
	char *unpacked_data = NULL, *pData = NULL;
	
	if (data == NULL || nCols <= 0 || nRows <= 0 || recordSizeInBytes <= 0) return NULL;
	
	unpacked_data = (char*) malloc(sizeof(char) * nRows * nCols * 2);
	if (unpacked_data == NULL) return NULL;

	// Unpack the genomic pedigree bit strings.
	for (i = 0, pData = data; i < nRows; i++, pData += recordSizeInBytes) {
		BitString bs = (BitString) pData;
		for (j = 0; j < nCols; j++) {
			long bitpos = (long) j * 2;
			unpacked_data[k++] = BitStringgetbit(bs, bitpos++);
			unpacked_data[k++] = BitStringgetbit(bs, bitpos);
		}
	}

	return unpacked_data;
}
